<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="80px">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="标题名称:" prop="configName">
						<el-input v-model="queryParams.configName" placeholder="请输入标题名称" clearable @clear="handleQuery"
							@keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="状态:" prop="status">
						<el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择状态" clearable>
							<el-option v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
								:label="dict.label" :value="dict.value" />
						</el-select>
					</el-form-item>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="轮播标题" align="center" prop="configName" />
			<el-table-column label="H5跳转URL" align="center" prop="urlAddress" />
			<el-table-column label="uniapp路由" align="center" prop="uniappRoute" />
			<el-table-column label="排序号" align="center" prop="sortNumber" />
			<el-table-column label="状态" align="center">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.hotel_statistics_status" :value="scope.row.status" />
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改岗位对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
			<div style="padding-right: 40px;">
				<el-form ref="form" :model="form" :rules="rules" label-width="120px">
					<el-form-item label="轮播标题:" prop="configName">
						<el-input v-model="form.configName" placeholder="请输入轮播标题" />
					</el-form-item>
					<el-form-item label="H5跳转URL:" prop="urlAddress">
						<el-input v-model="form.urlAddress" placeholder="请输入H5跳转URL" />
					</el-form-item>
					<el-form-item label="uniapp路由:" prop="uniappRoute">
						<el-input v-model="form.uniappRoute" placeholder="请输入uniapp路由" />
					</el-form-item>
					<el-form-item label="轮播图片:" prop="logoPicAddress">
						<image-upload :isShowTip="true" :height="208.5" :width="375" v-model="form.logoPicAddress" :limit="99"></image-upload>
					</el-form-item>
					<el-form-item label="启用状态:" prop="status">
						<el-radio-group v-model="form.status">
							<el-radio v-for="dict in dict.type.hotel_statistics_status" :key="dict.value"
								:label="dict.value">
								{{dict.label}}
							</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="排序号:" prop="sortNumber">
						<el-input v-model.number="form.sortNumber" placeholder="请输入排序号" />
					</el-form-item>
					<el-form-item label="备注:" prop="remark">
						<el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
					</el-form-item>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm" :loading="btnLoading">{{form.configId?'保 存':'确 定'}}
				</el-button>
				<el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		restaurantList,
		restaurantAdd,
		restaurantEdit,
		restaurantDetails,
		restaurantDelete
	} from "@/api/runSetup/content"
	export default {
		dicts: ['hotel_statistics_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				btnLoading: false,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				diLogLoading: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					configType: 0,
					configName: undefined,
					status: undefined
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					configName: {
						required: true,
						message: "请输入轮播标题",
						trigger: "blur"
					},
					logoPicAddress: {
						required: true,
						message: "请上传轮播图片",
						trigger: "blur"
					},
					status: {
						required: true,
						message: "请选择状态",
						trigger: "blur"
					},
					sortNumber: {
						required: true,
						message: "请输入排序号",
						trigger: "blur"
					}
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				restaurantList({
					...this.queryParams,
					configType: 2
				}).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					configType: 0,
					configName: undefined,
					urlAddress: undefined,
					uniappRoute: undefined,
					logoPicAddress: undefined,
					status: '0',
					sortNumber: undefined,
					remark: undefined
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.configId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.title = "新增";
				this.open = true;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				let postId = row.postId || this.ids
				if (Array.isArray(postId)) postId = postId.toString()
				this.title = "编辑";
				this.open = true;
				this.diLogLoading = true
				restaurantDetails(postId).then(response => {
					for (let i in this.form) this.form[i] = response.data[i]
					this.form.configId = postId
					this.diLogLoading = false
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.btnLoading = true
						if (this.form.configId != undefined) {
							restaurantEdit(this.form).then(response => {
								this.$modal.msgSuccess("编辑成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						} else {
							restaurantAdd({
								...this.form,
								configType: 2
							}).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.btnLoading = false
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.goodsId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的内容，是否要继续？').then(function() {
					return restaurantDelete(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/post/export', {
					...this.queryParams
				}, `post_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
